package cn.wenhe9.novel.manager;

import cn.wenhe9.novel.constant.CacheConsts;
import cn.wenhe9.novel.constant.ResultCodeEnum;
import cn.wenhe9.novel.domain.dto.UserInfoDto;
import cn.wenhe9.novel.domain.entity.user.UserInfo;
import cn.wenhe9.novel.domain.vo.UserInfoUpdateVo;
import cn.wenhe9.novel.exception.NovelException;
import cn.wenhe9.novel.mapper.UserInfoMapper;
import cn.wenhe9.novel.result.ResultResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

/**
 * 用户信息 缓存管理类
 *
 * @author DuJinliang
 * 2022/5/18
 */
@Component
public class UserInfoCacheManager {

    @Resource
    private UserInfoMapper userInfoMapper;

    /**
     * 查询用户信息，并放入缓存中
     */
    @Cacheable(
            value = CacheConsts.USER_INFO_CACHE_NAME,
            cacheManager = CacheConsts.REDIS_CACHE_MANAGER
//            keyGenerator = "keyGenerator"
    )
    public UserInfoDto getUser(Long userId) {
        UserInfo userInfo = userInfoMapper.selectById(userId);
        if(Objects.isNull(userInfo)){
            return null;
        }
        return UserInfoDto.builder()
                .id(userInfo.getId())
                .nickName(userInfo.getNickName())
                .userPhoto(userInfo.getUserPhoto())
                .userSex(userInfo.getUserSex())
                .accountBalance(userInfo.getAccountBalance()).build();
    }

    @CacheEvict(
            cacheNames = CacheConsts.USER_INFO_CACHE_NAME,
            cacheManager = CacheConsts.REDIS_CACHE_MANAGER,
            allEntries = true
//            keyGenerator = "keyGenerator"
    )
    public void updateUserInfo(Long userId, UserInfoUpdateVo vo) {
        UserInfo userInfo = new UserInfo();
        userInfo.setId(userId);
        userInfo.setNickName(vo.getNickName());
        userInfo.setUserPhoto(vo.getUserPhoto());
        userInfo.setUserSex(vo.getUserSex());
        userInfoMapper.updateById(userInfo);
    }

    @CacheEvict(
            cacheNames = CacheConsts.USER_INFO_CACHE_NAME,
            cacheManager = CacheConsts.REDIS_CACHE_MANAGER,
            allEntries = true
//            keyGenerator = "keyGenerator"
    )
    public void updatePassword(Long userId, String oldPassword, String newPassword1) {
        UserInfo tempUser = userInfoMapper.selectById(userId);
        if (!tempUser.getPassword().equals(DigestUtils.md5DigestAsHex(oldPassword.getBytes(StandardCharsets.UTF_8)))){
            throw new NovelException(ResultCodeEnum.OLD_PASSWORD_ERROR);
        }
        UserInfo userInfo = new UserInfo();
        userInfo.setId(userId);
        userInfo.setPassword(DigestUtils.md5DigestAsHex(newPassword1.getBytes(StandardCharsets.UTF_8)));
        userInfoMapper.updateById(userInfo);
    }

}
